package com.example.app.manager;

import android.content.Context;
import android.content.SharedPreferences;

import com.example.app.entity.UserProfile;

public class TokenManager {
    private static final String PREFS_NAME = "AppPrefs";
    private static final String KEY_TOKEN = "auth_token";
    private static final String KEY_USERNAME = "username";
    private static final String KEY_EMAIL = "email";
    private static final String KEY_USER_ID = "user_id";

    private static final String KEY_RESERVATION_ID = "reservationId";
    private final SharedPreferences prefs;

    public TokenManager(Context context) {
        prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    }

    public void saveToken(String token) {
        prefs.edit().putString(KEY_TOKEN, token).apply();
    }

    public String getToken() {
        return prefs.getString(KEY_TOKEN, null);
    }

    public void clearToken() {
        System.out.println("token被清除");
        prefs.edit().remove(KEY_TOKEN).apply();
    }

    public void saverReservationId(String token) {
        prefs.edit().putString(KEY_RESERVATION_ID, token).apply();
    }

    public String getReservationId() {
        return prefs.getString(KEY_RESERVATION_ID, null);
    }

    public void clearReservationId() {
        prefs.edit().remove(KEY_RESERVATION_ID).apply();
    }

    public void saveUserProfile(UserProfile profile) {
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(KEY_USERNAME, profile.getUsername());
        editor.putString(KEY_EMAIL, profile.getEmail());
        editor.putString(KEY_USER_ID, profile.getUserId());
        editor.apply();
    }

    public String getUsername() {
        return prefs.getString(KEY_USERNAME, null);
    }

    public String getEmail() {
        return prefs.getString(KEY_EMAIL, null);
    }

    public String getUserId() {
        return prefs.getString(KEY_USER_ID, null);
    }

    public void clearAllData() {
        prefs.edit()
                .remove(KEY_TOKEN)
                .remove(KEY_USERNAME)
                .remove(KEY_EMAIL)
                .remove(KEY_USER_ID)
                .remove(KEY_RESERVATION_ID)
                .apply();
    }
}